package com.tpl.core.basedata.action.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tpl.core.JsonUtil;
import com.tpl.core.action.BaseAction;
import com.tpl.core.action.SessionUtil;
import com.tpl.core.service.DataDictService;
import com.tpl.core.basedata.service.DeptService;
import com.tpl.core.common.DateUtil;
import com.tpl.core.common.TreeHelper;
import com.tpl.core.common.ValidateUtil;
import com.tpl.core.entity.SessionUser;
import com.tpl.core.entity.sys.DataDict;
import com.tpl.core.entity.sys.Dept;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 说明 :部门管理视图层
 * @author <a href="mailto:3380084@gmail.com">张坤雷</a>
 * @version 创建时间：2013-7-2 下午5:25:04 
 *
 */
@Controller
@RequestMapping("/dept")
public class DeptAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3006671953788480256L;
	@Autowired
	private DeptService deptService;
	@Autowired
	private DataDictService dataDictService;
	/**
	 * 获取部门树列表
	 * @param flag
	 */
	@RequestMapping({"/deptManageTree.do","/getDeptTree.do"})
	public @ResponseBody void getDeptTree(final boolean flag){

		final SessionUser user=getLoginUser();
		Long orgId = null;//user.getOrg().getId();
		Long userDId = user.getDept().getId();
		final Long deptId=user.getDept().getParentId();
		List<Dept> alldept=deptService.getDepts(null, null,null);
		List<Dept> depts=new ArrayList<Dept>();
		List<DataDict> dataDictList = dataDictService.getDictMenusByKey("deptType");
		Map<String, String> typeMap = new HashMap<>();
		typeMap.put("0", "分中心");
		for (DataDict dataDict : dataDictList) {
			typeMap.put(dataDict.getCode(), dataDict.getValue());
		}
		for(Dept d : alldept)
			if(!d.getParentId().equals(deptId)||d.getId().equals(userDId))
			{
				if(d.getType()!=null)
				{
					d.setTreeName(d.getDeptName());//+"["+typeMap.get(d.getType().toString())+"]");
				}else
				{
					d.setTreeName(d.getDeptName());
				}
				depts.add(d);
			}				
		TreeHelper tree=new TreeHelper(new String[]{"id","treeName","deptName","deptNo","parentId","admin","telPhone","fax","type","address","sortNum"},depts,4) {
			@Override
			public Object root() {
				if(orgId==null)
					return flag?new Object[]{deptId,"部门管理"}:deptId;
				else
					return new Object[]{deptId,"一级部门"};
			}
			@Override
			public Object[] idTextData(Object[] nodeData) {
				return new Object[]{nodeData[0],nodeData[1]};
			}
//			@Override
//			protected void setJsonAttributesData(JSONObject jo, String[] keys,
//					Object[] nodeData) {
//				super.setJsonAttributesData(jo, keys, nodeData);
//				jo.put("expand", expandService.getExpand(ExpandEnum.DEPT, jo.getLong("id")));
//			}
		};
		outJson(tree);
	}
	
	
	
	@RequestMapping("/saveOrUpdate.do")
	public @ResponseBody void saveDept(Dept dept,String[] txls,Long[] delTxl){
		ValidateUtil.validLength(dept.getDeptName(), 2, 30, "部门名称");
		ValidateUtil.validNumber(dept.getDeptNo(), "部门编码");
		ValidateUtil.validRequired(dept.getType(), "部门类型");
		dept.setUpdateDate(DateUtil.now());
		dept.setUpdator(SessionUtil.getLoginUserNo());
		dept.setCreateDate(DateUtil.now());
		dept.setCreator(SessionUtil.getLoginUserNo());
		deptService.doSave(dept);
		if(null!=delTxl&&delTxl.length>0){
			deptService.delTxls(delTxl);
		}
//		clearCache(dept.getId());
		outJsonSuccess();
	}
	@RequestMapping("/getAddressBook.do")
	public @ResponseBody void getAddressBook(Long deptId){
		List<JSONObject> abList= new ArrayList<>();//deptService.getAddressBook(deptId);
		JSONArray ja = JsonUtil.toJSONArray(abList);
		outJsonSuccess(ja.toString());
	}
	@RequestMapping("/doDel.do")
	public @ResponseBody void doDelete(Long id){
		deptService.doDelete(id);
		outJsonSuccess();
	}
//	/**
//	 * 清除缓存
//	 */
//	private void clearCache(Long deptId){
//		List<User> users=deptUserService.getUserByDeptId(deptId);
//		for(User u : users)
//		MemCacheUtil.delete(ICacheConstants.CACHE_AUTH_USER_DEPT+u.getId());
//	}
	@RequestMapping("/getDept.do")
	public String getDept(){
		return "dept/deptManage";
	}
	@RequestMapping("/editDept.do")
	public String editDept(){
		List<DataDict> cataList=dataDictService.getDictMenusByKey("deptType");
		putInRequest("cataList",cataList);
		return "dept/deptManage";
	}
}
